<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row>
          <a-col :span="24">
            <a-form-item label="工程编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-select-project
                v-decorator="['engineCode',validatorRules.engineCode]"
                :trigger-change="true" :multiple="false"
                org-fields="projectCode,projectName"
                dest-fields="engineCode,engineName"
                code="tj_project"
                @callback="Callback"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="工程名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['engineName', validatorRules.engineName]" placeholder="请输入工程名称" disabled></a-input>
            </a-form-item>
          </a-col>
<!--          <a-col :span="24">-->
<!--            <a-form-item label="制作班组id" :labelCol="labelCol" :wrapperCol="wrapperCol" hidden>-->
<!--              <a-input v-decorator="['workGroupId']" placeholder="请输入制作班组id"></a-input>-->
<!--            </a-form-item>-->
<!--          </a-col>-->
<!--          <a-col :span="24">-->
<!--            <a-form-item label="制作班组" :labelCol="labelCol" :wrapperCol="wrapperCol">-->
<!--              <j-popup-->
<!--                v-decorator="['workGroupName']"-->
<!--                :trigger-change="true"-->
<!--                org-fields="id,work_group_name"-->
<!--                dest-fields="workGroupId,workGroupName"-->
<!--                code="tj_workgroup"-->
<!--                @callback="popupCallback"/>-->
<!--            </a-form-item>-->
<!--          </a-col>-->
          <a-col :span="24">
            <a-form-item label="派工日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择派工日期" v-decorator="['dispatchTime']" :trigger-change="true" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="交付日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择交付日期" v-decorator="['payTime']" :trigger-change="true" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="构件附件" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-upload v-decorator="['frameAccessory']"></j-upload>
            </a-form-item>
          </a-col>

          <a-col :span="24">
            <a-form-item label="需求附件" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-upload v-decorator="['requirementAccessory']"></j-upload>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JUpload from '@/components/jeecg/JUpload'
  import JDate from '@/components/jeecg/JDate'
  import JSelectProject from '@/views/components/JSelectProject'
  import Vue from 'vue'
  import { ACCESS_TOKEN } from "@/store/mutation-types"

  export default {
    name: "EngineeringModal",
    components: {
      JDate,JSelectProject,JUpload
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        validatorRules: {
          engineCode: {
            rules: [
              { required: true, message: '请输入工程编号!'},
              { validator: (rule, value, callback) => validateDuplicateValue('engineering', 'engine_code', value, this.model.id, callback)},
            ]
          },
          engineName: {
            rules: [
              { required: true, message: '请输入工程名称!'},
            ]
          },
        },
        url: {
          add: "/engine/engineering/add",
          edit: "/engine/engineering/edit",
        },
      }
    },
    created () {
      const token = Vue.ls.get(ACCESS_TOKEN);
      this.headers = {"X-Access-Token":token};
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'engineCode','engineName','workGroupId','workGroupName','dispatchTime','payTime','frameAccessory','requirementAccessory'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }

        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'engineCode','engineName','workGroupId','workGroupName','dispatchTime','payTime','frameAccessory','requirementAccessory'))
      },
      Callback(row){
        this.form.setFieldsValue({
          engineCode:row[0].projectCode,
          engineName:row[0].projectName
        })
      }
    }
  }
</script>